package com.dci.service.impl.ms;

import com.dci.dao.ms.MsMenuInfoMapper;
import com.dci.dao.ms.MsRoleUserInfoMapper;
import com.dci.model.ms.Meta;
import com.dci.model.ms.MsMenuInfo;
import com.dci.model.ms.MsRoleUserInfo;
import com.dci.model.ms.dto.MsMenuInfoDTO;
import com.dci.result.JsonData;
import com.dci.service.ms.MsRoleUserInfoService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Copyright: 2019 姑苏区政府大数据中心
 * @Title: null.java
 * @Package com.acp.service.impl
 * @Description: 用户菜单管理
 * @author: 许高峰
 * @date: 2019/5/15 19:43
 */
@Service
public class MsRoleUserInfoServiceImpl implements MsRoleUserInfoService {

    @Resource
    private MsRoleUserInfoMapper msRoleUserInfoMapper;

    @Resource
    private MsMenuInfoMapper msMenuInfoMapper;

    public List<MsMenuInfoDTO> queryMenuAll(String userId, String systemType) {
        String id = msRoleUserInfoMapper.queryIdByUserId(userId);
        MsRoleUserInfo msRoleUserInfo = msRoleUserInfoMapper.queryRoleIdByUserId(id);
        List<MsMenuInfo> msMenuInfos = msMenuInfoMapper.queryMenuList(msRoleUserInfo.getRoleId(), systemType);
        List<MsMenuInfoDTO> msMenuInfoDTOList = Lists.newArrayList();
        msMenuInfos.forEach(item -> {
            Meta meta = Meta.builder().title(item.getName()).icon(item.getIcon()).build();
            MsMenuInfoDTO msMenuInfoDTO = MsMenuInfoDTO.builder().build();
            if (item.getHidden() == null || "".equals(item.getHidden())) {
                msMenuInfoDTO = MsMenuInfoDTO.builder()
                        .id(item.getId())
                        .pid(item.getParentId())
                        .path(item.getPath())
                        .component(item.getComponent())
                        .redirect(item.getRedirect())
                        .meta(meta).build();
            } else {
                msMenuInfoDTO = MsMenuInfoDTO.builder()
                        .id(item.getId())
                        .pid(item.getParentId())
                        .path(item.getPath())
                        .component(item.getComponent())
                        .redirect(item.getRedirect())
                        .hidden(item.getHidden())
                        .meta(meta).build();
            }
            msMenuInfoDTOList.add(msMenuInfoDTO);
        });
        return msMenuInfoDTOList;
    }

    public List<MsMenuInfoDTO> queryChild(Integer id, List<MsMenuInfoDTO> msMenuInfoList, String userId) {
        List<MsMenuInfoDTO> childList = Lists.newArrayList();
        msMenuInfoList.forEach(item -> {
            if (item.getPid() != 0) {
                if (item.getPid().equals(id)) {
                    childList.add(item);
                }
            }
        });
        childList.forEach(itemChild -> {
            itemChild.setChildren(queryChild(itemChild.getId(), msMenuInfoList, userId));
        });
        // 递归条件退出
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }


    @Override
    public JsonData queryMenuByUserId(String userId, String systemType) {
        String userName = msRoleUserInfoMapper.queryUserInfo(userId);
        // 插叙所有菜单数据
        List<MsMenuInfoDTO> msMenuInfos = queryMenuAll(userId, systemType);
        List<MsMenuInfoDTO> msMenuInfoList = Lists.newArrayList();
        msMenuInfos.forEach(item -> {
            // 查询一级菜单 一级菜单没有pid
            if (item.getPid() == 0) {
                msMenuInfoList.add(item);
            }
        });
        msMenuInfoList.forEach(itemMenu -> {
            itemMenu.setChildren(queryChild(itemMenu.getId(), msMenuInfos, userId));
        });
        return JsonData.success(msMenuInfoList,userName);
    }
}
